﻿using LuShanTea.DTO;
using LuShanTea.Front.Web.App_Start;
using LuShanTea.Front.Web.Models;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Front.Web.Controllers
{
    public class CartController : Controller
    {
        public ICartService CartService { get; set; }
        public ICartRecordService CartRecordService { get; set; }
        public IGoodService GoodService { get; set; }
        public IPackService PackService { get; set; }

        public IGoodImsSrcService GoodImgSrcService { get; set; }

        public IAddressService AddressService { get; set; }

        // GET: Cart
        [IsLogin]
        public ActionResult Index()
        {
            var userId = (long)Session["USERID"];

            var cartId = CartService.GetCartIdByUserId(userId);

            CartIndexModel model = new CartIndexModel();

            model.CartRecordDTOs = CartRecordService.GetNoBuyCartByCartId(cartId);
            model.GoodImgSrcDTOs = GoodImgSrcService.GetFirstImgByGoodId(model.CartRecordDTOs.Select(e => e.GoodId));

            return View(model);
        }

        [HttpPost]
        [IsLogin]
        public ActionResult UpdateCartGoodCount(CartUpdateGoodCountModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }
            var cartRecord = CartRecordService.GetById(model.CartRecordId);
            if (cartRecord == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "找不到该商品，请刷新试试" });
            }
            CartRecordService.UpdateCount(model.CartRecordId, model.Count);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        [IsLogin]
        public ActionResult Delete(long CartRecordId)
        {
            if (CartRecordId < 1 || CartRecordId > long.MaxValue)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "找不到该商品,请刷新试试" });
            }
            var cartRecord = CartRecordService.GetById(CartRecordId);
            if (cartRecord == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "找不到该商品,请刷新试试" });
            }
            CartRecordService.MarkDelete(CartRecordId);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        [IsLogin]
        public ActionResult BathDelete(long[] CartRecordIds)
        {
            if (CartRecordIds == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未选择商品" });
            }
            using (TransactionScope ts = new TransactionScope())
            {
                foreach (var id in CartRecordIds)
                {
                    if (id < 1 || id > long.MaxValue)
                    {
                        return Json(new AjaxResult { Status = "error", ErrorMsg = "错误" });
                    }
                    CartRecordService.MarkDelete(id);
                }
                ts.Complete();
                return Json(new AjaxResult { Status = "ok" });
            }
        }

        [IsLogin]
        [HttpPost]
        public ActionResult BuyGood(long[] CartRecordIds)
        {
            if (CartRecordIds == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未选择商品" });
            }
            if (AddressService.GetAddressByUserCount((long)Session["USERID"])<1)
            {
                return Json(new AjaxResult { Status = "redirect", ErrorMsg = "您没有收货地址，请先添加收货地址在进行购买" ,Data="/UserAddress/Index"});
            }

            using (TransactionScope ts = new TransactionScope())
            {
                foreach (var id in CartRecordIds)
                {
                    if (id < 1 || id > long.MaxValue)
                    {
                        return Json(new AjaxResult { Status = "error", ErrorMsg = "商品不存在" });
                    }
                    CartRecordService.BuyGood(id);
                }
                ts.Complete();
                return Json(new AjaxResult { Status = "ok" });
            }

        }

        [HttpPost]
        [IsLogin]
        public ActionResult AddCart(AddCartModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }
            if (GoodService.GetByGoodId(model.GoodId) == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "商品不存在" });
            }
            if (PackService.GetById(model.PackId) == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "包装不存在" });
            }
            var userId = (long)Session["USERID"];
            long cartId = CartService.GetCartIdByUserId(userId);
            CartRecordService.AddNew(cartId,model.GoodId,model.PackId,model.Count);
            return Json(new AjaxResult { Status = "ok" });
        }

    }
}